;   This program is free software: you can redistribute it and/or modify
;   it under the terms of the GNU General Public License as published by
;   the Free Software Foundation, either version 3 of the License, or
;   (at your option) any later version.
;
;   This program is distributed in the hope that it will be useful,
;   but WITHOUT ANY WARRANTY; without even the implied warranty of
;   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;   GNU General Public License for more details.
;
;   You should have received a copy of the GNU General Public License
;   along with this program.  If not, see <http://www.gnu.org/licenses/>.
;
;程序名称:2014easy.asm
;功能:2014年历简化版
;环境:16BIT DOS实模式(windows/dos或dosbox)
;编译器:MASM 5.1-6X 
;用法:看说明
;返回值:没有
;破坏寄存器:不适用
;
;原题163楼
;http://tieba.baidu.com/p/3740277941?pn=5
;这个简化版以之前的2014cal.asm做蓝本
;利用2014/1/1是星期三,和
;month_days db 31,28,31,30,31,30,31,31,30,31,30,31
;这两个资料计算其余的月份，不用int21函数，
;背景和其他复杂的技术,
;只加了计算月份的count_month_day而已
;

default_week    equ     3
esc_key         equ     01h
year_header_color equ   4eh
month_header_color equ  3dh
day_color       equ     3fh
title_att       equ     1eH
bottom_att      equ     1aH
first_lineY     equ     3
second_lineY    equ     12
first_lineX     equ     2
second_lineX    equ     26
Line_gap        equ     26
m1y             equ     first_lineY
m1x             equ     first_lineX
m2y             equ     first_lineY
m2x             equ     Line_gap + first_lineX
m3y             equ     first_lineY
m3x             equ     Line_gap + Line_gap + first_lineX
m4y             equ     second_lineY
m4x             equ     first_lineX
m5y             equ     second_lineY
m5x             equ     Line_gap + first_lineX
m6y             equ     second_lineY
m6x             equ     Line_gap + Line_gap + first_lineX

        DOSSEG
        .MODEL  small
        .data

month_days      db      31,28,31,30,31,30,31,31,30,31,30,31
month           db      37 dup (0)
curret_month_day dw     0
curret_week     db      default_week  ;2014/1/1 = 3
month_pointer   db      0       ;1
half_year_pointer db    0
Current_last_key db 28
year_head       db      '2014 / '
month_head      db      'September     '
head_L equ      $ - offset year_head
month_L equ     $ - offset month_head
day_buffer      db      head_L * 7  dup (0)
total_L equ     $ - offset year_head
total_Lx equ    $ - offset month_head

week_head db   ' S  M  T  W  T  F  S '

Title_str       db '<<<<   Calendar   >>>>',0
title_L equ     $ - offset title_str
bottom_str      db  'Space to Change Half-year, Esc to Exit'
bottom_L equ    $ - offset bottom_str

day_str         db ' 1  2  3  4  5  6  7 '
                db ' 8  9 10 11 12 13 14 '
                db '15 16 17 18 19 20 21 '
                db '22 23 24 25 26 27 28 '
                db '29 30 31 '

month_str label byte
        db 'January       ','February      ','March         ','April         '
        db 'May           ','June          ','July          ','August        '
        db 'September     ','October       ','November      ','December      '

att_table       db year_header_color
                db month_header_color
                db day_color
                db day_color
                db day_color
                db day_color
                db day_color
                db day_color

posXY label byte
                db      m1x, m1y
                db      m2x, m2y
                db      m3x, m3y
                db      m4x, m4y
                db      m5x, m5y
                db      m6x, m6y
                position_x      db 0
                position_y      db 0
                month_count     dw 0
                color_line      db 0
                set_flag        db 0

.stack 200h
.code

start:          mov     ax,@data
                mov     ds,ax
                mov     es,ax
                call    cls
                mov     cx,3030h           ;close cursor
                mov     ah,01h
                int     10h
                call    print_title
                mov     month_pointer,0
s5:             mov     month_count,0
s10:            call    count_month_day
                mov     di,offset month_head
                mov     cx,total_Lx
                mov     al,' ' ;space
                cld
                rep     stosb
                mov     al,month_pointer
                cbw
                mov     cx,month_L
                mul     cx
                add     ax,offset month_str
                mov     si,ax
                mov     di,offset month_head
                rep     movsb
                mov     si,offset week_head
                mov     di,offset day_buffer
                mov     cx,head_L
                rep     movsb
                ;
                mov     al,curret_week  ;2014/1/1 = 3
                mov     ah,al
                add     al,al
                add     al,ah
                cbw
                add     di,ax
                mov     al,Current_last_key
                mov     ah,0
                mov     cx,ax
                add     cx,cx
                add     cx,ax
                mov     si,offset day_str
                rep     movsb
                mov     cx,8
                mov     si,month_count
                shl     si,1
                add     si,offset posXY
                mov     dx,[si]
                mov     bp,offset year_head
                mov     color_line,0
s40:
                push    cx
                call    print_day
                pop     cx
                add     bp,head_L
                inc     color_line
                inc     dh
                loop    s40
                ;
                inc     month_count
                cmp     month_count,6
                jae     s45
                inc     month_pointer
                jmp     s10

s45:            mov     ah,0
                int     16h
                cmp     ah,esc_key
                jz      quit
                cmp     ah,39h          ;space
                jnz     s45
                xor     half_year_pointer,1
                test    half_year_pointer,1
                mov     month_pointer,0
                jz      s52
                mov     month_pointer,6
s52:            jmp     s5

quit:           call    cls
                mov     cx,0c0dh  ;open cursor
                mov     ah,01
                int     10h
                mov     ah,4ch
                int     21h
;-------------------------------------------------------------
print_day:      mov     cx,head_L
                mov     al,color_line
                mov     bx,offset att_table
                xlatb
                mov     bl,al      ;get color attrib
                mov     bh,0
                mov     ax,1300h
                int     10h
                ret
;--------------------------------------------------------
cls:    	mov	ah,0fh                  ;get display mode to al
		int	10h
		mov	ah,0                    ;Set display mode
		int	10h
		ret
;--------------------------------------------------------
print_title:    push    es
                push    ds
                pop     es
                mov     cx,title_L
                mov     bl,title_att
                mov     bh,0
                mov     bp,offset Title_str
                mov     dl,27
                mov     dh,1
                mov     ax,1300h
                int     10h
                ;
                mov     cx,bottom_L
                mov     bl,bottom_att
                mov     bh,0
                mov     bp,offset bottom_str
                mov     dl,20
                mov     dh,22
                mov     ax,1300h
                int     10h
                ;
                pop     es
                ret
;---------------------------------------------------------------------------
;month_days      db      31,28,31,30,31,30,31,31,30,31,30,31

count_month_day:
                cmp     month_pointer,0
                jnz     cm20
                mov     curret_week,default_week ;3
		mov	al,month_pointer
                mov     ah,0
                mov     bx,offset month_days
                xlatb
                mov     Current_last_key,al
		ret

cm20:           mov     al,month_pointer
                mov     bx,offset month_days
                xlatb
                mov     Current_last_key,al
                mov     al,month_pointer
                dec     al
                xlatb
                mov     ah,0
                sub     al,28
                add     al,curret_week
                cmp     al,6
                jbe     cm40
                sub     al,7
cm40:           mov     curret_week,al
                ret
;---------------------------------------------------------------------------
                end     start
 
